﻿using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;
using System.Web.UI.WebControls;
using Foxy.GridViewControls;

namespace Foxy.WebControls
{
    public partial class GridView : System.Web.UI.WebControls.GridView
    {
        [Category("ExtendedProperties")]
        public event RowClickEventHandler RowClick
        {
            add { base.Events.AddHandler(EventRowClick, value); }
            remove { base.Events.RemoveHandler(EventRowClick, value); }
        }

        [Category("ExtendedProperties")]
        public event RowDoubleClickEventHandler RowDoubleClick
        {
            add { base.Events.AddHandler(EventRowDoubleClick, value); }
            remove { base.Events.RemoveHandler(EventRowDoubleClick, value); }
        }

        [Category("ExtendedProperties"), Description("Occurs when the value of the CheckBox.Checked property changes between posts the server")]
        public event RowCheckedEventHandler CheckBoxChecked
        {
            add { base.Events.AddHandler(EventRowCheckBoxChecked, value); }
            remove { base.Events.RemoveHandler(EventRowCheckBoxChecked, value); }
        }

        protected virtual void OnRowClick(RowClickEventArgs e)
        {
            GridViewRow row = e.GridViewRow;
            CheckBox chkBox = (CheckBox)row.FindControl(CheckBoxItem.CheckBoxID);
            if (chkBox.Checked)
                this.SelectedIndex = row.RowIndex;

            RowClickEventHandler evntHandler = (RowClickEventHandler)base.Events[EventRowClick];
            if (evntHandler != null)
                evntHandler(this, e);
        }

        protected virtual void OnRowDoubleClick(RowDoubleClickEventArgs e)
        {
            GridViewRow row = e.GridViewRow;
            CheckBox chkBox = (CheckBox)row.FindControl(CheckBoxItem.CheckBoxID);
            if (chkBox.Checked)
                this.SelectedIndex = row.RowIndex;

            RowDoubleClickEventHandler evntHandler = (RowDoubleClickEventHandler)base.Events[EventRowDoubleClick];
            if (evntHandler != null)
                evntHandler(this, e);
        }
        /// <summary>
        /// Occurs when the value of the CheckBox.Checked property changes between posts the server
        /// </summary>
        /// <param name="e">Event arguments sended with the RowChecked element.</param>
        /// <seealso cref="RowCheckedEventArgs"/>
        protected virtual void OnCheckBoxCheckedChanged(RowCheckedEventArgs e)
        {
            GridViewRow row = e.GridViewRow;
            RowCheckedEventHandler evntHandler = (RowCheckedEventHandler)base.Events[EventRowCheckBoxChecked];
            if (evntHandler != null)
                evntHandler(this, e);
        }
    }
}
